﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IronCow.UnitTests.Mock
{
    public class MockListResponsePart : MockResponsePart
    {
        private string mId;
        private string mName;
        private string mFilter;
        private TaskListFlags mFlags;

        public MockListResponsePart(string name)
            : this(123456789, name)
        {
		}

		public MockListResponsePart(string name, string filter)
			: this(123456789, name, filter)
		{
		}

		public MockListResponsePart(int id, string name)
			: this(id, name, TaskListFlags.None)
		{
		}

		public MockListResponsePart(int id, string name, string filter)
			: this(id, name, TaskListFlags.Smart, filter)
		{
		}

		public MockListResponsePart(int id, string name, TaskListFlags flags)
            : this(id, name, flags, null)
        {
        }

		public MockListResponsePart(int id, string name, TaskListFlags flags, string filter)
        {
            mId = id.ToString();
            mName = name;
            mFilter = filter;
            mFlags = flags;
        }

        public override void GetResponseMarkup(System.Xml.XmlWriter markupWriter, Dictionary<string, string> parameters)
        {
            int deleted = GetFlagMarkupValue(TaskListFlags.Deleted);
            int locked = GetFlagMarkupValue(TaskListFlags.Locked);
            int archived = GetFlagMarkupValue(TaskListFlags.Archived);
            int smart = GetFlagMarkupValue(TaskListFlags.Smart);

            markupWriter.WriteStartElement("list");
            markupWriter.WriteAttributeString("id", mId);
            markupWriter.WriteAttributeString("name", mName);
            markupWriter.WriteAttributeString("deleted", deleted.ToString());
            markupWriter.WriteAttributeString("locked", locked.ToString());
            markupWriter.WriteAttributeString("archived", archived.ToString());
            markupWriter.WriteAttributeString("position", "0");
            markupWriter.WriteAttributeString("smart", smart.ToString());
            markupWriter.WriteElementString("filter", mFilter);
            markupWriter.WriteEndElement();
        }

        private int GetFlagMarkupValue(TaskListFlags flag)
        {
            return ((mFlags & flag) == flag) ? 1 : 0;
        }
    }
}
